package com.example.neusoft.cook.fragment;


import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.neusoft.cook.MainActivity;
import com.example.neusoft.cook.R;
import com.example.neusoft.cook.activity.RegsiterActivity;
import com.example.neusoft.cook.base.BaseFragment;
import com.example.neusoft.cook.bean.SampleLogin;
import com.example.neusoft.cook.interf.Config;
import com.example.neusoft.cook.interf.RetrofitService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static android.app.Activity.RESULT_CANCELED;
import static android.app.Activity.RESULT_OK;

/**
 * A simple {@link Fragment} subclass.
 */
public class LoginFragment extends BaseFragment {
    private EditText usernameL;
    private  EditText userpasswordL;
    private Button loginL;
    private  Button regL;



    public LoginFragment() {
        // Required empty public constructor
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        usernameL = (EditText) view.findViewById(R.id.usernamel);
        userpasswordL = (EditText) view.findViewById(R.id.userpasswordl);
        loginL = (Button) view.findViewById(R.id.loginl);
        regL = (Button) view.findViewById(R.id.regL);

        loginL.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                //retrofit 连接服务器 发送登录请求
                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl(Config.BASE_URL)
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                RetrofitService retrofitService =
                        retrofit.create(RetrofitService.class);

                String usernamel = usernameL.getText().toString();
                String userpasswordl = userpasswordL.getText().toString();

                Context context = getActivity();
                Intent intent = new Intent();

                Call<SampleLogin> call =
                        retrofitService.userLogin(usernamel,userpasswordl);
                call.enqueue(new Callback<SampleLogin>() {
                    @Override
                    public void onResponse(Call<SampleLogin> call, Response<SampleLogin> response) {
                        SampleLogin loginResult = response.body();
                        if (loginResult.getUserid().equals("0")) {
                            Toast.makeText(getActivity(), "用户名或密码错误", Toast.LENGTH_SHORT).show();
                        }else{

                            //显示主页面
                            Toast.makeText(getActivity(),"登录成功",Toast.LENGTH_SHORT).show();

                            MainActivity mainActivity =
                                    (MainActivity)getActivity();

                            if (mainActivity !=null){
                                mainActivity.replaceLoginFragment();
                                //保存登录状态
                                mainActivity.setSavedUserId(loginResult.getUserid());//保存到文件
                                setUserId(loginResult.getUserid());//保存到程序
                            }

                        }
                    }

                    @Override
                    public void onFailure(Call<SampleLogin> call, Throwable t) {

                    }
                });
            }
        });



        regL.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                //1、启动注册页面

                Intent intent = new Intent(getActivity(), RegsiterActivity.class);
                startActivityForResult(intent, 1 );

            }
        });
    }

    //添加返回值的回调方法
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode ==1 ){
            switch (resultCode){
                case RESULT_OK:
                    String username = data.getStringExtra("username");
                    usernameL.setText(username);
                    break;
                case  RESULT_CANCELED:
                    break;
            }
        }


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_login, container, false);
    }

}
